package com.kaizeli.website.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaizeli.website.mapper.CompanyMapper;
import com.kaizeli.website.pojo.dto.base.PageDTO;
import com.kaizeli.website.pojo.entity.CompanyDO;
import com.kaizeli.website.pojo.vo.CompanyVO;
import com.kaizeli.website.query.CompanyQuery;
import com.kaizeli.website.service.CompanyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 公司服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CompanyServiceImpl implements CompanyService {
    
    private final CompanyMapper companyMapper;
    
    @Override
    public PageDTO<CompanyVO> pageQuery(CompanyQuery query) {
        Page<CompanyDO> page = new Page<>(query.getPageNum(), query.getPageSize());
        
        QueryWrapper<CompanyDO> queryWrapper = new QueryWrapper<>();
        if (query.getName() != null && !query.getName().isEmpty()) {
            queryWrapper.like("name", query.getName());
        }
        if (query.getCode() != null && !query.getCode().isEmpty()) {
            queryWrapper.eq("code", query.getCode());
        }
        if (query.getStatus() != null && !query.getStatus().isEmpty()) {
            queryWrapper.eq("status", query.getStatus());
        }
        queryWrapper.orderByDesc("id");
        
        Page<CompanyDO> result = companyMapper.selectPage(page, queryWrapper);
        return PageDTO.of(result, CompanyVO.class);
    }
    
    @Override
    public CompanyVO getById(Integer id) {
        CompanyDO companyDO = companyMapper.selectById(id);
        if (companyDO == null) {
            return null;
        }
        return convertToVO(companyDO);
    }
    
    @Override
    public CompanyVO getByCode(String code) {
        QueryWrapper<CompanyDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", code);
        CompanyDO companyDO = companyMapper.selectOne(queryWrapper);
        if (companyDO == null) {
            return null;
        }
        return convertToVO(companyDO);
    }
    
    @Override
    public Integer save(CompanyDO companyDO) {
        companyMapper.insert(companyDO);
        return companyDO.getId();
    }
    
    @Override
    public Boolean update(CompanyDO companyDO) {
        return companyMapper.updateById(companyDO) > 0;
    }
    
    @Override
    public Boolean deleteById(Integer id) {
        return companyMapper.deleteById(id) > 0;
    }
    
    @Override
    public List<CompanyVO> listByStatus(String status) {
        List<CompanyDO> list = companyMapper.selectList(
            new QueryWrapper<CompanyDO>().eq("status", status)
        );
        return list.stream().map(this::convertToVO).collect(Collectors.toList());
    }
    
    @Override
    public List<CompanyVO> listAll() {
        List<CompanyDO> list = companyMapper.selectList(null);
        return list.stream().map(this::convertToVO).collect(Collectors.toList());
    }
    
    @Override
    public List<CompanyVO> search(String keyword) {
        QueryWrapper<CompanyDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", keyword).or().like("code", keyword);
        List<CompanyDO> list = companyMapper.selectList(queryWrapper);
        return list.stream().map(this::convertToVO).collect(Collectors.toList());
    }
    
    /**
     * 转换为VO
     */
    private CompanyVO convertToVO(CompanyDO companyDO) {
        CompanyVO vo = new CompanyVO();
        vo.setId(companyDO.getId());
        vo.setName(companyDO.getName());
        vo.setCode(companyDO.getCode());
        vo.setStatus(companyDO.getStatus());
        return vo;
    }
}

